package com.youmingxing.bean.selfinfo;

import com.youmingxing.bean.AuditionFromUser;
import com.youmingxing.bean.Content;
import com.youmingxing.data.GlobalDataManager;

import org.json.JSONArray;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

/**
 * JHS
 * 15/9/19
 * 我的主页
 */
public class PersonalInfo {

    public String nickname;
    public String headimg;
    public String motto;//格言
    public int popularity;//人气
    public int support_ticket;//加油票
    public int follower;//粉丝
    public int follow;//关注者
    public boolean bindQQ;
    public boolean bindWeibo;
    public boolean bindWechat;
    public boolean bindMobile;
    public int real_auth_status;//实名认证状态: 0未认证1待审核2认证未通过3认证通过
    public AuditionFromUser latestAudition;
    public boolean haveMoreAutidion = false;
    public List<Content> myContents;
    public int auditions_badge;
    public int real_auth_badge;

    public PersonalInfo(JSONObject response) {
        int userId = response.optInt("id");
        GlobalDataManager.userId = userId;
        GlobalDataManager.save();
        nickname = response.optString("nickname");
        headimg = response.optString("headimg");
        motto = response.optString("motto");
        popularity = response.optInt("popularity");
        support_ticket = response.optInt("support_ticket");
        follower = response.optInt("follower");
        follow = response.optInt("follow");
        auditions_badge = response.optInt("auditions_badge");
        real_auth_badge = response.optInt("real_auth_badge");
        String str[] = response.optString("login_providers").replace("[", "").replace("]", "").split(",");
        for (String aStr : str) {
            switch (aStr) {
                case "\"qq\"":
                    bindQQ = true;
                    break;
                case "\"wechat\"":
                    bindWechat = true;
                    break;
                case "\"weibo\"":
                    bindWeibo = true;
                    break;
                case "\"mobile\"":
                    bindMobile = true;
                    break;
            }
        }
        real_auth_status = response.optInt("real_auth_status");
        JSONArray auditions = response.optJSONArray("auditions");
        if (auditions.length() > 0) {
            latestAudition = new AuditionFromUser((JSONObject) auditions.opt(0));
        }
        if (auditions.length() > 1) {
            haveMoreAutidion = true;
        }
        JSONArray contents = response.optJSONArray("update");
        myContents = new ArrayList<>();
        if (contents != null) {
            myContents.add(new Content(null));
            for (int i = 0; i < contents.length(); i++) {
                Content content = new Content(contents.optJSONObject(i));
                myContents.add(content);
            }
        }
    }
}
